package cn.hznc.controller.service.permission;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.permission.EmployeePermissionReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.mapper.EmployeeMapper;
import cn.hznc.mapper.EmployeePermissionMapper;
import cn.hznc.mapper.RolesMapper;
import cn.hznc.mapper.RolesPermissionMapper;
import cn.hznc.repository.UserRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class EmployeePermissionService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private RolesPermissionMapper rolesPermissionMapper;
    @Resource
    private EmployeePermissionMapper employeePermissionMapper;
    @Resource
    private RolesMapper rolesMapper;
    @Resource
    private UserRepository userR;

//    @Resource
//    private RolesEntity rolesEntity;
//    @Resource
//    private RolesConnectionEntity rolesConnectionEntity;
//    @Resource
//    private EmployeePermissionEntity employeePermissionEntity;

    public PageResult<EmployeePermissionEntity> queryPageByEmployeeName(Integer pageSize, Integer pageNO, String employeeName){
        AtomicInteger flag= new AtomicInteger();
        int startPage = (pageNO-1)* pageSize;
        int endPage = pageNO* pageSize;
        AtomicInteger count= new AtomicInteger();

        List<Integer> list = new ArrayList<>();
        LambdaQueryWrapper<EmployeeEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (employeeName != "") {
            queryWrapper.like(EmployeeEntity::getEmployeeName, employeeName);
        }
        LambdaQueryWrapper<UserConnectionEntity> permissionQueryWrapper = new LambdaQueryWrapper<>();
        permissionQueryWrapper.ne(UserConnectionEntity::getUserId,"");

        List<UserConnectionEntity> userConnectionEntityList = employeePermissionMapper.selectList(permissionQueryWrapper);
//        int i = 0;
        Map<Integer,List<UserConnectionEntity>> map = userConnectionEntityList.stream().collect(Collectors.groupingBy(UserConnectionEntity::getUserId));
//        PageHelper.startPage(pageNO, pageSize);
        List<EmployeeEntity> employeeEntityList = employeeMapper.selectList(queryWrapper);
        List<EmployeePermissionEntity> employeePermissionEntityList = new ArrayList<>();
        employeeEntityList.forEach(employeeEntity -> {
            List<UserConnectionEntity> userConnectionEntityList1 = map.get(employeeEntity.getUserId());
            if(CollectionUtil.isNotEmpty(userConnectionEntityList1)){
                userConnectionEntityList1.forEach(userConnectionEntity -> {
                    list.add(1);
                    count.getAndIncrement();
                    if (flag.get() <endPage){
                        flag.getAndIncrement();
                    }
//                if (employeePermissionEntityList.size()<pageSize){
                    if (flag.get()>startPage && employeePermissionEntityList.size()<pageSize){
                        RolesEntity rolesEntity = rolesMapper.selectById(userConnectionEntity.getRolesId());
                        EmployeePermissionEntity employeePermissionEntity = new EmployeePermissionEntity();

                        employeePermissionEntity.setUserName(userR.selectbyUserid(employeeEntity.getUserId()).getUsername());
                        employeePermissionEntity.setId(employeeEntity.getUserId());
                        employeePermissionEntity.setEmployeeName(employeeEntity.getEmployeeName());
                        employeePermissionEntity.setRolesName(rolesEntity.getRolesName());
                        employeePermissionEntity.setDepartment(employeeEntity.getDepartment());
                        employeePermissionEntity.setCompany(employeeEntity.getCompany());
                        employeePermissionEntity.setRolesId(rolesEntity.getId());
                        employeePermissionEntityList.add(employeePermissionEntity);
                    }
//                }

                });
            }

        });

        List<EmployeePermissionEntity> pageList = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(employeePermissionEntityList)){
//            pageList = CommonUtil.pageList(employeePermissionEntityList , pageNO , pageSize);
            PageInfo<EmployeePermissionEntity> result = new PageInfo<>(employeePermissionEntityList);
            return new PageResult<>(result.getList(), (long)list.size());


//        }
//        return new PageResult<>(pageList ,(long)employeePermissionEntityList.size());
//        PageInfo<EmployeePermissionEntity> result = new PageInfo<>(employeePermissionEntityList);
//        return new PageResult<>(result.getList(), result.getTotal());
    }

    public Object deleteEmployeeByUserId(Integer userId){
        LambdaQueryWrapper<EmployeeEntity> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(EmployeeEntity::getUserId,userId);
        LambdaQueryWrapper<UserConnectionEntity> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(UserConnectionEntity::getUserId,userId);
        employeePermissionMapper.delete(queryWrapper2);
        employeeMapper.delete(queryWrapper1);
        userR.deleteByUserId(userId);
        return Boolean.TRUE;
    }

    public List<RolesEntity> queryAllRoles(){
        LambdaQueryWrapper<RolesEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<RolesEntity> rolesEntityList = rolesMapper.selectList(queryWrapper);
        return rolesEntityList;
    }

    public List<EmployeeEntity> queryAllEmployee(){
        LambdaQueryWrapper<EmployeeEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<EmployeeEntity> employeeEntityList = employeeMapper.selectList(queryWrapper);
        return employeeEntityList;
    }

    public Object updateEmployee(EmployeePermissionReq employeePermissionReq){
        LambdaQueryWrapper<EmployeeEntity> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(EmployeeEntity::getUserId,employeePermissionReq.getUserId());
        EmployeeEntity employeeEntity = employeeMapper.selectOne(queryWrapper1);
        employeeEntity.setCompany(employeePermissionReq.getCompany());
        employeeEntity.setDepartment(employeePermissionReq.getDepartment());
        employeeMapper.updateById(employeeEntity);
        UpdateWrapper<UserConnectionEntity> updateWrapper = new UpdateWrapper<>();
//        queryWrapper2.set("userId",employeePermissionReq.getUserId());
//        queryWrapper2.set("rolesId",employeePermissionReq.getNewRolesId());
        updateWrapper.eq("user_id",employeePermissionReq.getUserId());
        updateWrapper.eq("roles_id",employeePermissionReq.getLowRolesId());
//        UserConnectionEntity userConnectionEntity1 = employeePermissionMapper.selectOne(queryWrapper2);
        UserConnectionEntity userConnectionEntity = new UserConnectionEntity();
        userConnectionEntity.setUserId(employeePermissionReq.getUserId());
        userConnectionEntity.setRolesId(employeePermissionReq.getNewRolesId());

        employeePermissionMapper.update(userConnectionEntity,updateWrapper);
//        employeePermissionMapper.up
        return Boolean.TRUE;
    }

    public Object updatePassword(Integer id,String password){
        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EmployeeEntity::getId,id);
        EmployeeEntity employeeEntity = employeeMapper.selectOne(lambdaQueryWrapper);
//        LambdaQueryWrapper<UserEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper1.eq(UserEntity::getId,employeeEntity.getUserId());
        UserEntity userEntity = userR.selectbyUserid(employeeEntity.getUserId());
        if (password != null){
            String password1 = DigestUtils.md5DigestAsHex((password).getBytes());
            userEntity.setPassword(password1);
            userR.updatePassword(userEntity);
        }
        return Boolean.TRUE;

    }
    public EmployeeInfoManageEntity queryEmployeeInfo(Integer userId){
        EmployeeInfoManageEntity entity = new EmployeeInfoManageEntity();
        entity.setUserId(userId);
        LambdaQueryWrapper<EmployeeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EmployeeEntity::getUserId,userId);
        EmployeeEntity employeeEntity = employeeMapper.selectOne(lambdaQueryWrapper);
        entity.setEmployeeId(employeeEntity.getId());
        if (StringUtils.isNotEmpty(employeeEntity.getEmployeeNo())){
            entity.setEmployeeNo(employeeEntity.getEmployeeNo());
        }
        if (StringUtils.isNotEmpty(employeeEntity.getEmployeeName())){
            entity.setEmployeeName(employeeEntity.getEmployeeName());
        }
        if (StringUtils.isNotEmpty(employeeEntity.getCompany())){
            entity.setCompany(employeeEntity.getCompany());
        }
        if (StringUtils.isNotEmpty(employeeEntity.getDepartment())){
            entity.setDepartment(employeeEntity.getDepartment());
        }
        LambdaQueryWrapper<UserConnectionEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(UserConnectionEntity::getUserId,userId);
        List<UserConnectionEntity> userConnectionEntityList = employeePermissionMapper.selectList(lambdaQueryWrapper1);
        List<String> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userConnectionEntityList)){
            userConnectionEntityList.forEach(userConnectionEntity -> {
                LambdaQueryWrapper<RolesEntity> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper2.eq(RolesEntity::getId,userConnectionEntity.getRolesId());
                RolesEntity rolesEntity = rolesMapper.selectOne(lambdaQueryWrapper2);
                list.add(rolesEntity.getRolesName());
            });
        }
        entity.setRolesName(list);
        return entity;
    }
}
